package com.sziit.service;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Hashtable;

/**
 * Function: 二维码生成过程，先生成二维码矩阵数据，再将矩阵数据转换成图片写入磁盘
 * Author: he
 * Date: 2019/9/24 11:17
 * Notice:
 **/
@Service
public class CodeService {

    public static void main(String[] args) {
        System.out.println(System.getProperty("user.dir"));
    }

    /**
     * Function:生成二维码的业务层方法，参数text表示二维码所包含的数据
     * Author: he
     * Date: 2019/9/24 11:26
     * Notice:
     **/
    public String returnUrlPath(String text) {
        //定义文件名，取当前时间的毫秒值作为文件名
        String fileName = System.currentTimeMillis()+"";

        //定义生成二维码图片格式
        String format = "png";
        try {
            //生成二维码矩阵数据
            BitMatrix bitMatrix = CreateMatrix(text);
            //定义图片保存到resources下面的iamges中
            String savaPath = System.getProperty("user.dir") + "/src/main/resources/images/";
            File outputFile = new File(savaPath + fileName +"."+ format);
            writeToFile(bitMatrix, format, outputFile);
            System.out.println("生成二维码成功");

        } catch (WriterException e) {
            e.printStackTrace();
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            System.out.println(sw.toString());
        } finally {
            //返回二维码图片的网络访问路径
            String relativePath = "http://localhost:8081/myService/images/" + fileName + ".png";
            return relativePath;
        }
    }

    /**
     *Function:生成二维码矩阵数据
     *Author: he
     *Date: 2019/9/24 12:23
     *Notice:
     **/
    public BitMatrix CreateMatrix(String text) throws Exception{
        //定义二维码编码方式
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //定义图片的大小
        int width = 400;
        int height = 400;
        //生成二维码矩阵数据
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        return bitMatrix;
    }

    /**
     * Function:将二维码数据写入磁盘
     * Author: he
     * Date: 2019/9/24 11:19
     * Notice:
     **/
    public void writeToFile(BitMatrix matrix, String format, File file)
            throws IOException {
        int BLACK = 0xFF000000;
        int WHITE = 0xFFFFFFFF;
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }
}
